Skip to content

BUG: fix: list as index item does not raise #61674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Andre-Andreati
Copy link

Problem:

Index constructor was allowing creation of indexes where one of the index's item is a list (unhashable) while others are not lists.

  • If all items are lists - like columns=[ ['a', 'b'], ['b', 'c'], ['b', 'c'] ], it will try to create a MultiIndex. Correct.
  • If any item is a list, but NOT all - like the initial example, columns=[ 'a', ['b', 'c'], ['b', 'c'] ], there's no check for this condition, and the creation will follow as if all items, including the lists, are valid column names.

Solution:

Added a check in the Index constructor for this case. Raise ValueError.
Added test to check if is raising correctly.
Changed a test that should raise.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Comment on lines +570 to +573
# 60925 should raise when one of index's items is a list and others are not
if any(isinstance(el, list) for el in data) and not all(
isinstance(el, list) for el in data
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No non-hashable value should occur here. Instead, can you do this check on arr below, and only in the case it's (a) a NumPy array (not an ExtensionArray) and (b) of object dtype. Use pandas.core.dtypes.inference.is_hashable.

@@ -71,9 +71,12 @@ def test_assign_index_sequences(self):
repr(df)

# this travels an improper code path
# 60925 should raise when one of index's items is a list and others are not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use GH#60925 throughout.

Comment on lines +78 to +79
df.index = index
repr(df)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it should raise on the first line, is that right? If so, can you remove repr(df).

@@ -749,6 +749,7 @@ Indexing
- Bug in :meth:`MultiIndex.insert` when a new value inserted to a datetime-like level gets cast to ``NaT`` and fails indexing (:issue:`60388`)
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)
- Bug in reindexing of :class:`DataFrame` with :class:`PeriodDtype` columns in case of consolidated block (:issue:`60980`, :issue:`60273`)
- Bug in creating :class:`Index` with one item being a ``list`` among others that aren't (should raise ValueError) (:issue:`60925`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Bug in creating :class:`Index` with one item being a ``list`` among others that aren't (should raise ValueError) (:issue:`60925`)
- Bug in creating :class:`Index` with some items being a non-hashable; this now raises a ``ValueError`` (:issue:`60925`)

@rhshadrach rhshadrach added Bug Index Related to the Index class or subclasses labels Jun 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Index Related to the Index class or subclasses
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Index.drop_duplicates() is inconsistent for unhashable values
2 participants